import http from '@ohos.net.http';

import Log from '../utils/Log';
import api from './api'
import { objectToQueryString } from '../utils/Tool'

const host = "http://iot.simemg.com:8765"

type f<T> = (_?: Object) => Promise<string | Object | T | ArrayBuffer>
const get = <T>(url: string, params?: any): Promise<string | Object | T | ArrayBuffer> => {
    Log.debug("GET请求链接："+ url + objectToQueryString(params))
    return http.createHttp().request(host + url + objectToQueryString(params), {method: http.RequestMethod.GET}).then(res => {
        return res.result
    })
}
const post = <T>(url: string, params?: any): Promise<string | Object | T | ArrayBuffer> => {
    Log.debug("POST请求链接："+ url + "，参数：" + JSON.stringify(params))
    return http.createHttp().request(host + url, {method: http.RequestMethod.POST, extraData: params}).then(res => {
        return res.result
    })
}


export const BookSearch:f<any> = _ => get(api.book_search, _)
export const BookSearchName:f<any> = _ => get(api.book_search_name, _)
export const BookSearchChapter:f<any> = _ => get(api.book_search_chapter, _)
export const BookSearchInfo:f<any> = _ => get(api.book_search_info, _)

export const NovelSearchContent:f<any> = _ => get(api.novel_search_content, _)
export const ComicSearchContent:f<any> = _ => get(api.comic_search_content, _)

export const HotWordSearch:f<any> = _ => get(api.hot_word, _)

